//
//  DataProvider.m
//  CWeekly
//
//  Created by Tiger on 12-4-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DataProvider.h"
#import "SBJSON.h"

//#define REQUEST_URL @"http://192.168.1.121:8080/Server/%@?%@"
//#define REQUEST_URL @"http://113.57.134.91:18080/cifnews/%@?%@"
#define REQUEST_URL @"%@%@?%@"


@implementation DPData

@synthesize isSuccess, data;

- (void)dealloc {
	[super dealloc];
}

- (id)initWithDPData:(DPData *)theData {
    self = [super init];
    if (self) {
        self.data = theData.data;
		self.isSuccess = theData.isSuccess;
    }
    return self;
}

@end


@implementation DataProvider

static DataProvider * sharedInstance = nil;

+ (DataProvider*) sharedManager {
	@synchronized(self) {
		if (sharedInstance == nil) {
			[[self alloc] init]; // assignment not done here
		}
	}
	return sharedInstance;
}

- (id) init
{
	if ( [super init] ) {
		sharedInstance = self;
	}
	
	return self;
}

- (void)dealloc {
    [super dealloc];
}


- (DPData *)_requestWithAction:(NSString *)action body:(NSString *)body check:(BOOL)isChecked{
	
	NSURL *url;
    NSMutableURLRequest *request;
	NSMutableString *strUrl;
	DPData *dpDataReturn;
    
    url = nil;
	request = nil;
	dpDataReturn = [[DPData alloc]init];
    
    if (action) {
        strUrl = [[NSMutableString alloc] initWithFormat:REQUEST_URL,SERVER,action,body];
		
//		if (isChecked) {
//			NSString *strName = [NSString stringWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"Username"]];
//			NSString *strPwd = [NSString stringWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"Password"]];
//			[strUrl appendFormat:@"&name=%@&passwd=%@",strName,strPwd];
//		}
        
		url = [[NSURL alloc] initWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
		
		//NSLog(@"%@ - url:%@",action,url);
        
        request = [[NSMutableURLRequest alloc] initWithURL:url];
		[request setTimeoutInterval:20.0];
        [url release];
		[strUrl release];
        
        [request setHTTPMethod:@"GET"];
        
        NSError *error;
        NSURLResponse *urlResponse;
        NSData *urlData;
		
		urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
		
		[request release];
        
        NSString *responseString;
        responseString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
        
        NSString *str = [responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSLog(@"%@ - response string:%@",action,str);
		
		SBJSON *json = [[SBJSON new] autorelease];
        id obj = [json objectWithString:str error:nil];
		
		if ([obj isKindOfClass:[NSDictionary class]] && [[obj objectForKey:@"status"] intValue] == 1) {
			dpDataReturn.isSuccess = YES;
			dpDataReturn.data = obj;
		}
		else {
			dpDataReturn.isSuccess = NO;
			if (![obj objectForKey:@"message"]) {
                dpDataReturn.data = @"获取服务器数据失败";
            }
            else {
                dpDataReturn.data = [NSString stringWithFormat:@"%@",[obj objectForKey:@"message"]];
            }
		}
		
		[responseString release];
    }
	
	return [dpDataReturn autorelease];
}


- (DPData *)getBookList
{
	DPData *dpData;
    
    NSString *strBody;
	strBody = [[NSString alloc] initWithString:@"cmd=1001&param=0"];
	DPData *dpDataReturn = [self _requestWithAction:@"SynMgController" body:strBody check:NO];
	[strBody release];
	
	dpData = [[DPData alloc] initWithDPData:dpDataReturn];
    
    return [dpData autorelease];
}

- (DPData *)getMarkList{
    DPData *dpData;
    NSString *strBody;
	strBody = [[NSString alloc] initWithString:@"cmd=1007"];
	DPData *dpDataReturn = [self _requestWithAction:@"SynMgController" body:strBody check:NO];
	[strBody release];
	
	dpData = [[DPData alloc] initWithDPData:dpDataReturn];
    
    return [dpData autorelease];
}

- (DPData*)sendMark:(NSString*)markBody{
    DPData *dpData;
    NSString *strBody;
	strBody = [[NSMutableString alloc] initWithString:@"cmd=1008"];
    [strBody stringByAppendingFormat:@"&%@",markBody];
	DPData *dpDataReturn = [self _requestWithAction:@"SynMgController" body:strBody check:NO];
	[strBody release];
	
	dpData = [[DPData alloc] initWithDPData:dpDataReturn];
    
    return [dpData autorelease];

}
@end
